package tms.model.login;

import tms.model.profile.UserProfileEntity;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "USER")
@NamedQueries({
        @NamedQuery(name = "UserEntity.getByName", query = "select u from UserEntity u where u.name = :name"),
        @NamedQuery(name = "UserEntity.getAll", query = "select u from UserEntity u"),
        @NamedQuery(name = "UserEntity.getWithWorkDayEnded",
                query = "select u " +
                        "from UserEntity u, TimeEntity t " +
                        "where u.id = t.userId " +
                            "and t.runTime >= u.profile.workDayLength " +
                            "and t.startTime >= :dayBegin and t.startTime <= :dayEnd " +
                            "and t.isRunning = true "),
        @NamedQuery(name = "UserEntity.getUnnotifiedWithWorkDayEnded",
                query = "select u " +
                        "from UserEntity u, TimeEntity t " +
                        "where u.id = t.userId " +
                            "and t.runTime >= u.profile.workDayLength " +
                            "and t.startTime >= :dayBegin and t.startTime <= :dayEnd " +
                            "and t.isRunning = true " +
                            "and t.isUserNotified = false ")
})
public class UserEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Integer id;

    @Column(name = "PROFILE_ID", insertable = false, updatable = false)
    private Integer profileId;

    @Column(name = "NAME")
    private String name;

    @Column(name = "PASSWORD")
    private String password;
    
    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "PROFILE_ID", referencedColumnName = "ID")
    private UserProfileEntity profile;


    public UserEntity() {
    }

    public UserEntity(String name, String password) {
        this.name = name;
        this.password = password;
        profile = new UserProfileEntity();
    }
    

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public UserProfileEntity getProfile() {
        return profile;
    }

    public Integer getProfileId() {
        return profileId;
    }
}
